#include "iobot.h"
#include "scancodes.h"
#include "syscall.h"

void iobot_process_scancode(iobot_t *bot, unsigned char scancode)
{
	//ignore the first byte of an extended code
	if(scancode == 0xE0)
	{
		return;
	}

   if (scancode & 0x80)
   {//key released.
		scancode &= ~0x80;//take away the break bit.
		switch(scancode)
		{
			case 54:
				bot->caps = !bot->caps;
				break;
			case 42:
				bot->caps = !bot->caps;
				break;
			case 29:
				bot->ctrl = 0;
				break;
			case 56:
				bot->alt = 0;
				break;
			default:

				break;
		}
   }
   else
   {//key pressed
		unsigned int res1, res2, res3, success = 0;//needed for the system calls
		switch(scancode)
		{
			case 58://caps lock
				bot->caps = !bot->caps;
				bot->capslock = !bot->capslock;
				syscall3(SYS_KBD_LEDS, bot->scrl, bot->num, bot->capslock, res1, res2, res3, success)
				break;
			case 69://num lock
				bot->num = !bot->num;
				syscall3(SYS_KBD_LEDS, bot->scrl, bot->num, bot->capslock, res1, res2, res3, success)
				break;
			case 70://scroll lock
				bot->scrl = !bot->scrl;
				syscall3(SYS_KBD_LEDS, bot->scrl, bot->num, bot->capslock, res1, res2, res3, success)
				break;
			case 54://shift
				bot->caps = !bot->caps;
				break;
			case 42://the other shift
				bot->caps = !bot->caps;
				break;
			case 29:
				bot->ctrl = 1;
				break;
			case 56:
				bot->alt = 1;
				break;
			default:
				if(bot->caps)
				{
					iobot_put_input_string(bot, us_shift[scancode]);
				}
				else//regular lowercase
				{
					iobot_put_input_string(bot, us_norm[scancode]);
				}
				break;
		}
   }
}
